package com.wsx.sky.http.controller;

import com.wsx.sky.http.bean.HttpRequest;
import com.wsx.sky.http.bean.HttpResponse;
import com.wsx.sky.http.model.HotUrls;
import com.wsx.sky.http.service.HttpServerService;
import com.wsx.sky.redis.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wangshuaixin
 * @Title: com.wsx.sky.http.controller.TraceServerController
 * @Description: TODO
 * @date 2018/12/10
 */
@RestController
@RequestMapping("/http/server")
public class TraceServerController {


    @Autowired
    private HttpServerService httpServerService;

    @Autowired
    private RedisService redisService;


    @RequestMapping(
            value = "/request/feign",
            method = {RequestMethod.POST},
            consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE},
            produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}
    )
    public HttpResponse post(@RequestBody HttpRequest httpRequest) {
        System.out.println(httpRequest.getTraceid());
        if (null == httpRequest
                || StringUtils.isEmpty(httpRequest.getName())) {
            return new HttpResponse().buildCode(30001).buildMsg("参数为空");
        }
        int num = httpServerService.save(httpRequest.getName());
        if (num == 1) {
            return new HttpResponse();
        }
        return new HttpResponse().buildCode(50001).buildMsg("数据库操作失败");
    }


    @RequestMapping(
            value = "/get/feign/{name}",
            method = {RequestMethod.GET},
            produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}
    )
    public HttpResponse get(@PathVariable("name") String name) {
        if (StringUtils.isEmpty(name)) {
            return new HttpResponse().buildCode(30001).buildMsg("参数为空");
        }
        String value = redisService.getString(name);
        if (StringUtils.isEmpty(value)) {
            value = httpServerService.get(name);
            if (!StringUtils.isEmpty(value)) {
                redisService.setString(name, value);
            }
        }
        if (StringUtils.isEmpty(value)) {
            return new HttpResponse().buildCode(30001).buildMsg("没有该数据");
        }
        return new HttpResponse();
    }


    @RequestMapping(
            value = "/get/batch",
            method = {RequestMethod.GET},
            produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}
    )
    public HttpResponse batch() {
        List<HotUrls> hotUrls = httpServerService.list();
        if (null != hotUrls && !hotUrls.isEmpty()) {
            for (HotUrls url : hotUrls) {
                String flow = url.getFlow();
                double num = getNum(flow);
                if (num <= 0) {
                    continue;
                }
                int id = url.getId();
                int succ = httpServerService.updateUrl(id, num);
                if (succ >= 1) {
                    System.out.println("id" + id + ":success");
                }
            }
        }
        return new HttpResponse().buildMsg("hello");
    }

    private double getNum(String data) {
        double num = 0.0;
        if (data.endsWith("GB")) {
            data = data.replace("GB", "");
            num = Double.valueOf(data) * 1024;
        } else if (data.endsWith("MB")) {
            data = data.replace("MB", "");
            num = Double.valueOf(data);
        }
        return num;
    }
}
